import { BaseClass } from "./BaseClass";
import { UIMgr, UIType } from "./UIMgr";
import { ResManager } from "./ResManager";
import { AbNames, EventMsgKes, GamePkg, HallPkg, LoadingPkg } from "../../../Game/Scripts/Config/ResConfig";
import { director } from "cc";
import { MyFunction } from "./ViewMgr";

export interface ICall {
    func: any,
    path: string
}

/**
 * 场景切换管理器，目前是单独一个场景， 就是界面切换
 */
export class SceneMgr extends BaseClass {
    // 值是Stype
    public gameView:number = 0; 

    public lastView = null;

    private static _inst: SceneMgr;
    public static get inst(): SceneMgr {
        if (this._inst == null) {
            this._inst = new SceneMgr();
        }
        return this._inst;
    }

    constructor() {
        super()    
        //加载通用的bundle
    }

    //显示大厅
    public showHall(){
        return UIMgr.inst.ShowUIView(AbNames.GameHall,'Prefabs/GameHall',UIType.Full)
    }

    //显示游戏界面
    public showGame(){
        return UIMgr.inst.ShowUIView(AbNames.GamePlay,'Prefabs/GamePlay/GamePlay',UIType.Full)
    }


    //进入登录界面1
    public async goToLoading(){
        // ResManager.inst.loadDirAssetboundle([LoadingPkg],(now: number, total: number)=>{},()=>{
        //     console.log("======loadAb Loading End========")
        //     UIMgr.inst.ShowUIView(AbNames.GameLoading,'Prefabs/GameLoading',1)
        // })
    }

    //加载大厅的资源
    public async loadHallRes(){
        // ResManager.inst.loadDirAssetboundle([HallPkg],(now: number, total: number)=>{
        //     var pre = (now / total);
        //     director.emit(EventMsgKes.loading,pre);
        // },()=>{
        //     console.log("======loadHallRes End========")
        //     director.emit(EventMsgKes.loadingEnd,"");
        // })
    }


    //从Loading进入Hall界面1
    public LoadingToHall() {
        //链接大厅服务
            
        let com = this.showHall();
        //移除loading包资源
        ResManager.inst.ReleaseAllAssetInBundle(AbNames.GameLoading);
        return com;
    }

    //大厅到游戏界面1
    public async HallToGame(callback: Function){
        ResManager.inst.loadDirAssetboundle([GamePkg],(now: number, total: number)=>{},()=>{
            console.log("======loadAb Games End========")
            let ts_node = this.showGame();
            callback && callback(ts_node);
            setTimeout(()=>{
                //移除大厅的资源，这里不消除
                // ResManager.inst.ReleaseAllAssetInBundle(AbNames.GameHall);   
            },500)
        })
    }


    //大厅进入游戏界面
    public goToGame(path: string,callback: Function = null){
        this.LoadGameRes(()=>{
            let ts_node = UIMgr.inst.ShowUIView(AbNames.GamePlay,path,1)
            callback && callback(ts_node);
            // 销毁大厅的资源
            // ResManager.inst.ReleaseAllAssetInBundle(AbNames.GameHall);
        });
    }

    //加载大厅资源
    public LoadHallRes(endCall:Function = null){
        ResManager.inst.loadDirAssetboundle([HallPkg],(now: number, total: number)=>{},()=>{
            console.log("======loadAb Hall End========")
            endCall && endCall();
        })
    }

    //加载游戏资源
    public LoadGameRes(endCall:Function = null){
        ResManager.inst.loadDirAssetboundle([GamePkg],(now: number, total: number)=>{},()=>{
            console.log("======loadAb Game End========")
            endCall && endCall();
        })
    }


    public SetCallFun(func:MyFunction){
        return function (path: string) {
            func(path);
        };

    }
    //主要是从游戏里面跳转
    //显示大厅的UI
    public showHallViwe(path: string){
        console.log('===========SceneMgr==showHallViwe===========',path)
        UIMgr.inst.ShowUIView(AbNames.GameHall,path,UIType.Full);
    }
}